package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGrouponRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/19 22:37
 */
@SuppressWarnings("all")
@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {

    @Autowired
    DtsGrouponRulesMapper dtsGrouponRulesMapper;

    @Autowired
    DtsGoodsService dtsGoodsService;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer goodsId) {

        IPage<DtsGrouponRules> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();

        //参数判断
        if(goodsId!=null){
            wrapper.eq("goods_id",goodsId);
        }
        //排序规则判断
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("deleted", false);

        dtsGrouponRulesMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items", iPage.getRecords());

        return map;
    }

    /**
     * 规则添加
     */
    @Override
    public void insert(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRules.setUpdateTime(new Date());
        dtsGrouponRules.setAddTime(new Date());
        dtsGrouponRules.setDeleted(false);
        DtsGoods dtsGoods =dtsGoodsService.findByGoodsId(dtsGrouponRules.getGoodsId());
        if(dtsGoods ==null ){
            throw new RuntimeException("无对应的商品，请修改ID");
        }
        dtsGrouponRules.setGoodsName(dtsGoods.getName());
        dtsGrouponRules.setPicUrl(dtsGoods.getPicUrl());

        dtsGrouponRulesMapper.insert(dtsGrouponRules);
    }

    @Override
    public void update(DtsGrouponRules dtsGrouponRules) {
        DtsGoods byGoodsId = dtsGoodsService.findByGoodsId(dtsGrouponRules.getGoodsId());
        if(byGoodsId == null ){
            throw new RuntimeException("无对应的商品,请修改ID");
        }
        dtsGrouponRules.setPicUrl(byGoodsId.getPicUrl());
        dtsGrouponRules.setGoodsName(byGoodsId.getName());
        dtsGrouponRules.setUpdateTime(new Date());
        dtsGrouponRulesMapper.updateById(dtsGrouponRules);
    }

    @Override
    public void delete(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRules.setDeleted(true);
        dtsGrouponRulesMapper.updateById(dtsGrouponRules);
    }

}
